package com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.repository.impl;

import com.css.fxfzypg.modules.suggestinfo.analysis.disasterscenario.repository.entity.DisasterScenarioEntity;
import com.css.fxfzypg.modules.suggestinfo.analysis.emergencydisaster.repository.entity.EmergencyDisasterEntity;
import com.css.fxfzypg.modules.suggestinfo.analysis.emergencydisposal.repository.entity.EmergencyDisposalEntity;
import com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.repository.SuggestionAnalysisRepository;
import com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.vo.SuggestionAnalysisParams;
import com.css.fxfzypg.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class SuggestionAnalysisRepositoryImpl implements SuggestionAnalysisRepository {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public Map<String, Object> queryTaskList(SuggestionAnalysisParams queryParams, int curPage, int pageSize, String userId) {
        StringBuilder sql = new StringBuilder("select DISTINCT t1.id,t1.task_num,t1.task_name,t1.task_manager,t1.task_status,t1.task_district,t1.create_time,t2.group_name " +
                "from ypg_preassess_task t1 left join ypg_group t2 on t1.group_id=t2.id left join ypg_group_user t3 on t3.group_id = t2.id ");
        StringBuilder whereSql = new StringBuilder(" where t1.del_flag = '0' and ((t1.id not in(select parent_id from ypg_preassess_task where  parent_id!='0') and t1.parent_id='0') or (t1.parent_id!='0' and t1.del_flag = '0')) and (t1.create_user = '"+userId+"' or t2.group_leader_id = '"+userId+"' or t3.user_id ='"+userId+ "')");
        if(!PlatformObjectUtils.isEmpty(queryParams.getTaskStatus())){
            whereSql.append(" and t1.task_status = '"+ queryParams.getTaskStatus()+"'");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getTaskNum())){
            whereSql.append(" and t1.task_num like '%"+ queryParams.getTaskNum() +"%'");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getTaskName())){
            whereSql.append(" and t1.task_name like '%"+ queryParams.getTaskName() +"%'");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getProvince()) || !PlatformObjectUtils.isEmpty(queryParams.getCity()) || !PlatformObjectUtils.isEmpty(queryParams.getArea())){
            if(!PlatformObjectUtils.isEmpty(queryParams.getProvince())){
                whereSql.append(" and t1.task_district like '%"+ queryParams.getProvince() +"%'");
            }
            if(!PlatformObjectUtils.isEmpty(queryParams.getCity())){
                whereSql.append(" and t1.task_district like '%"+ queryParams.getCity() +"%'");
            }
            if(!PlatformObjectUtils.isEmpty(queryParams.getArea())){
                whereSql.append(" and t1.task_district like '%"+ queryParams.getArea() +"%'");
            }
        }
        StringBuilder orderSql = new StringBuilder(" order by t1.create_time desc");
        int firstIndex=(curPage-1) * pageSize;
        int lastIndex=pageSize;
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        //StringBuilder groupSql = new StringBuilder(" group by t1.id,t1.task_num,t1.task_name,t1.task_manager,t1.task_status,t1.task_district,t2.group_name,t1.create_time ");
        sql.append(whereSql).append(orderSql);
        Integer total= jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(SuggestionAnalysisParams.class)).size();
        List<SuggestionAnalysisParams> suggestionAnalysisParamsList = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(SuggestionAnalysisParams.class));
        Map<String,Object> map = new HashMap();
        map.put("total",total);
        map.put("rows",suggestionAnalysisParamsList);
        return map;
    }

    @Override
    public List<DisasterScenarioEntity> querySuggestionByTaskNum(String taskNum) {
        String sql ="select * from ypg_disaster_scenario where  task_num='"+taskNum+"' and del_flag='0'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DisasterScenarioEntity.class));
    }

    @Override
    public List<EmergencyDisasterEntity> queryEmergencyByTaskNum(String taskNum) {
        String sql ="select * from ypg_emergency_disaster where  task_num='"+taskNum+"' and del_flag='0'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(EmergencyDisasterEntity.class));
    }

    @Override
    public List<EmergencyDisposalEntity> queryEmergencyDisposalByTaskNum(String taskNum) {
        String sql ="select * from ypg_emergency_disposal where  task_num='"+taskNum+"' and del_flag='0'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(EmergencyDisposalEntity.class));
    }
}
